package com.iwhalecloud.bss.kite.cucc.order.accept.chain.impl;

import com.iwhalecloud.bss.kite.common.constant.ServiceOfferConsts;
import com.iwhalecloud.bss.kite.cucc.order.accept.chain.AbstractOrderHandler;
import com.iwhalecloud.bss.kite.cucc.order.util.AcceptUtils;
import com.iwhalecloud.bss.kite.dataservice.cache.OfferCache;
import com.iwhalecloud.bss.kite.dataservice.cache.ProductCache;
import com.iwhalecloud.bss.kite.dataservice.entity.inst.OfferInst;
import com.iwhalecloud.bss.kite.dataservice.entity.inst.OfferObjInstRel;
import com.iwhalecloud.bss.kite.dataservice.entity.inst.ProdInst;
import com.iwhalecloud.bss.kite.dataservice.entity.spec.OfferDetail;
import com.iwhalecloud.bss.kite.dataservice.entity.spec.Product;
import com.iwhalecloud.bss.kite.manager.enums.ActionType;
import com.iwhalecloud.bss.kite.manager.enums.InstType;
import com.iwhalecloud.bss.kite.manager.enums.RelType;
import com.iwhalecloud.bss.kite.order.client.dto.InstContainer;
import com.iwhalecloud.bss.kite.order.client.dto.OrderRequest;
import com.iwhalecloud.bss.kite.order.client.proxy.InstContainerProxy;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.Objects;

@Component
public class ProdTransferHandler extends AbstractOrderHandler {

    @Override
    public boolean canHandle(OrderRequest orderRequest) {
        if (Objects.isNull(orderRequest)
            || !ServiceOfferConsts.TRANSFER_PROD_INST.equals(orderRequest.getServiceOfferId())) {
            return false;
        }

        return true;
    }

    @Override
    public void doHandle(OrderRequest request) {
        InstContainerProxy instContainerProxy = InstContainer.ofRequest(request);
        List<ProdInst> prodInsts = instContainerProxy.listProdInsts();
        for (ProdInst inst : prodInsts) {
            String compInstId = inst.getCompInstId();
            String newProdId = inst.getProdId();
            String prodInstId = inst.getProdInstId();
            Product product = ProductCache.get(newProdId);
            String newBaseOfferId = product.getBaseOfferId();
            OfferInst offerInst = request.getInstContainer().getOfferInstMap().get(compInstId);
            // 1.修改对应的基础销售品offerId为新的offerId
            AcceptUtils.addInstChange(request, offerInst.getOfferInstId(), InstType.BASE_OFFER_INST, ActionType.M,
                "offerId", offerInst.getOfferId(), newBaseOfferId);
            AcceptUtils.addInstChange(request, offerInst.getOfferInstId(), InstType.BASE_OFFER_INST, ActionType.M,
                "spare_fld0", offerInst.getOfferInstId(), offerInst.getOfferInstId());
            AcceptUtils.addInstChange(request, offerInst.getOfferInstId(), InstType.BASE_OFFER_INST, ActionType.M,
                "seq", "0", "0");
            AcceptUtils.setActionType(offerInst, ActionType.M.name());
            offerInst.setOfferId(newBaseOfferId);
            offerInst.setSpareFld0(offerInst.getOfferInstId());
            offerInst.setSeq("0");
            // 回填 offerInst
            request.getInstContainer().getOfferInstMap().put(compInstId, offerInst);

            // 2.原产品对应基础销售品的明细进行M,设置对应属性值更新
            OfferObjInstRel offerObjInstRel = InstContainer.ofRequest(request).getOfferObjInstRel(compInstId,
                prodInstId, RelType.OFFER_OBJ_REL_TYPE_1000.getCode());
            if (offerObjInstRel != null) {
                List<OfferDetail> offerProdDetails = OfferCache.get(newBaseOfferId).listAccessProdDetail();
                for (OfferDetail detail : offerProdDetails) {
                    if (detail.getObjId().equals(newProdId)) {
                        int maxNum = Integer.parseInt(detail.getMaxNum());
                        if (maxNum > 0) {
                            batchObjInstRelAttrUpdate(request, detail, compInstId, inst.getProdInstId(),
                                offerObjInstRel);
                            break;
                        }
                    }
                }
                InstContainer.ofRequest(request).addOfferObjInstRel(offerObjInstRel);
            }
        }
    }

    private void batchObjInstRelAttrUpdate(OrderRequest request, OfferDetail detail, String compInstId,
        String prodInstId, OfferObjInstRel offerObjInstRel) {
        AcceptUtils.addRelChange(request, compInstId, InstType.BASE_OFFER_INST, prodInstId, InstType.PROD_INST,
            RelType.OFFER_OBJ_REL_TYPE_1000, "prod_comp_role_cd", offerObjInstRel.getProdCompRoleCd(),
            detail.getRoleId());
        AcceptUtils.addRelChange(request, compInstId, InstType.BASE_OFFER_INST, prodInstId, InstType.PROD_INST,
            RelType.OFFER_OBJ_REL_TYPE_1000, "detail_id", offerObjInstRel.getDetailId(), detail.getDetailId());
        AcceptUtils.addRelChange(request, compInstId, InstType.BASE_OFFER_INST, prodInstId, InstType.PROD_INST,
            RelType.OFFER_OBJ_REL_TYPE_1000, "offer_obj_rel_id", offerObjInstRel.getOfferObjRelId(),
            detail.getDetailId());
        AcceptUtils.addRelChange(request, compInstId, InstType.BASE_OFFER_INST, prodInstId, InstType.PROD_INST,
            RelType.OFFER_OBJ_REL_TYPE_1000, "offer_prod_inst_rel_id", offerObjInstRel.getOfferProdInstRelId(),
            offerObjInstRel.getOfferObjInstRelId());
        AcceptUtils.setActionType(offerObjInstRel, ActionType.M.name());
        offerObjInstRel.setProdCompRoleCd(detail.getRoleId());
        offerObjInstRel.setDetailId(detail.getDetailId());
        offerObjInstRel.setOfferObjRelId(detail.getDetailId());
        offerObjInstRel.setOfferProdInstRelId(offerObjInstRel.getOfferProdInstRelId());
        offerObjInstRel.setUpdateDate(new Date());
    }
}
